Page({
  /**
   * 页面的初始数据
   */
  data: {
    peoples: "",
    phone: "",
    switch1: false,
    detailed: '请选择',
    num: '',
    userName: '',
    mobile: '',
    detil: '',
    province: '',   //省
    city: '',    //市
    county: '',    //区
    ids: '',    //添加地址或者选择地址带过来的参数
    id: '',
    paramsrequest: {},
    defaultAddress: 1,
    ajxtrue: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ids: options.ids
    })
    if (options.id) {
      this.setData({
        id: options.id
      })
      this.geteditlist();    //如果存在id就默认为编辑，从地址列表带过来id参数，否则就是添加
    }
  },
  geteditlist () {
    const request = {
      id: this.data.id,
    }
    wx.$API['workService/addressget'](request).then((response) => {
      console.log(response, 999999999)
      if (response.data[0].defaultAddress) {
        this.setData({
          switch1: true
        })
      }
      this.setData({
        userName: response.data[0].userName,
        mobile: response.data[0].mobile,
        detil: response.data[0].detil,
        province: response.data[0].province,
        city: response.data[0].city,
        county: response.data[0].county,
      })
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
    })

  },
  onChange (event) {
    const detail = event.detail;
    this.setData({
      'switch1': detail.value
    })
  },
  bindManual: function (e) {
    var userName = e.detail.value;
    // 将数值与状态写回
    this.setData({
      userName: userName,
    });
  },
  bindmobile (e) {
    var mobile = e.detail.value;
    let that = this
    if (!(/^1[34578]\d{9}$/.test(mobile))) {
      this.setData({
        ajxtrue: false
      })
      if (mobile.length >= 11) {
        wx.showToast({
          title: '手机号有误',
          icon: 'success',
          duration: 2000
        })
      }
    } else {
      this.setData({
        ajxtrue: true,
        mobile: mobile
      })
    }
  },
  bindaddress (e) {
    var detil = e.detail.value;
    this.setData({
      detil: detil
    })
  },
  bindRegionChange: function (e) {
    var that = this
    //为了让选择框有个默认值，    
    that.setData({
      clas: ''
    })　　　//下拉框所选择的值
    this.setData({
      //拼的字符串传后台
      detailed: e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2],
      //下拉框选中的值
      region: e.detail.value
    })
    this.setData({
      "AddSite.area": e.detail.value[0] + " " + e.detail.value[1] + " " + e.detail.value[2]
    })
    if (this.data.id) {
      this.setData({
        province: e.detail.value[0],
        city: e.detail.value[1],
        county: e.detail.value[2]
      })
    } else {
      this.setData({
        province: e.detail.value[0],
        city: e.detail.value[1],
        county: e.detail.value[2]
      })
    }

  },
  getlocation () {
    wx.showToast({
      title: '修改成功',
      icon: 'success',
      duration: 2000
    })
    wx.redirectTo({
      url: '../../workService/selectadaddress/index?selectadaddress=' + this.data.switch1
    })
  },
  getentrustPrint () {
    wx.navigateTo({
      url: '../../workService/entrustPrint/index?id=' + this.data.id
    })
  },
  getdefault () {    //设为默认地址接口
    const request = {
      userName: this.data.userName,
      mobile: this.data.mobile,
      detil: this.data.detil,
      province: this.data.province,
      city: this.data.city,
      county: this.data.county,
      defaultAddress: this.data.defaultAddress
    }
    wx.$API['workService/addressave'](request).then((response) => {
      if (this.data.ids == 0) {
        this.getlocation();
      }
      else if (this.data.ids == 1) {
        this.getentrustPrint();
      }
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
    })
  },
  /**
   * @see 保存地址
   */
  getAddlist () {
    var request = this.data.paramsrequest;
    wx.$API['workService/addressave'](request).then((response) => {
      if (this.data.ids == 0) {
        this.getlocation();
      }
      else if (this.data.ids == 1) {
        this.getentrustPrint();
      }
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
      console.log('保存地址error', error)
    })
  },
  getsaveditlist () {
    const request = {
      userName: this.data.userName,
      mobile: this.data.mobile,
      detil: this.data.detil,
      province: this.data.province,
      city: this.data.city,
      county: this.data.county,
      defaultAddress: false,
      id: this.data.id
    }
    wx.$API['workService/addressupdate'](request).then((response) => {
      if (this.data.ids == 0) {
        this.getlocation();
      }
      else if (this.data.ids == 1) {
        this.getentrustPrint();
      }
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
    })
  },
  getSwitch () {
    const request = {
      id: this.data.id,
      userName: this.data.userName,
      mobile: this.data.mobile,
      detil: this.data.detil,
      province: this.data.province,
      city: this.data.city,
      county: this.data.county,
      defaultAddress: this.data.defaultAddress
    }
    wx.$API['workService/addressupdate'](request).then((response) => {
      if (this.data.ids == 0) {
        this.getlocation();
      } else if (this.data.ids == 1) {
        this.getentrustPrint();
      }
    }).catch((error) => {
      wx.showToast({
        title: error,
        icon: "none"
      })
    })
  },
  getSavedit () {
    const request = {
      id: this.data.id,
      userName: this.data.userName,
      mobile: this.data.mobile,
      detil: this.data.detil,
      province: this.data.province,
      city: this.data.city,
      county: this.data.county
    }
    this.setData({
      paramsrequest: request
    })
    if (this.data.switch1 == true) {
      this.getSwitch();
    } else {
      this.getsaveditlist();
    }
  },
  savebtn: function () {
    if (this.data.id) {
      this.getSavedit();    //保存修改方法
    } else {
      const request = {
        userName: this.data.userName,
        mobile: this.data.mobile,
        detil: this.data.detil,
        province: this.data.province,
        city: this.data.city,
        county: this.data.county,
        defaultAddress: false
      }
      this.setData({
        paramsrequest: request
      })
      if (this.data.userName && this.data.mobile && this.data.detil && this.data.province && this.data.city && this.data.county != '') {
        if (this.data.switch1 == true) {
          this.getdefault();
        } else {
          this.getAddlist();
        }
      } else {
        wx.showToast({
          title: '地址不能为空，请输入地址',
          icon: 'none',
          duration: 2000
        })
      }
    }
  },
})